import java.io.BufferedReader;
import java.io.InputStreamReader;

public class AdbOperator {
    public static void pull(String androidPath, String conputerPath, String deviceID) {
        try {
            ProcessBuilder processBuilder = new ProcessBuilder("cmd", "/c","adb -s " + deviceID + " pull " +"\""+ androidPath +"\""+ " " +"\""+ conputerPath+ "\"");
            // 启动CMD命令
            Process process = processBuilder.start();
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
                while (reader.readLine() != null) {
                    // 可选择打印日志或忽略输出
                }
            }
            // 等待进程结束
            process.waitFor();
        } catch (Exception e) {
            e.printStackTrace(); // 捕获异常并打印
        }
    }

    public static void push(String conputerPath, String androidPath, String deviceID) {
        try {
            ProcessBuilder processBuilder = new ProcessBuilder("cmd", "/c","adb -s " + deviceID + " pull " +"\""+ conputerPath+"\""+ " " +"\""+ androidPath + "\"");
            // 启动CMD命令
            Process process = processBuilder.start();
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
                while (reader.readLine() != null) {
                    // 可选择打印日志或忽略输出
                }
            }
            // 等待进程结束
            process.waitFor();
        } catch (Exception e) {
            e.printStackTrace(); // 捕获异常并打印
        }
    }

    public static String getFileUpdateTime(String path,String fileName,String deviceID) {
        String output = "";
        try {
            // 创建ProcessBuilder对象
            ProcessBuilder processBuilder = new ProcessBuilder();
            // 设置CMD命令
            processBuilder.command("cmd", "/c", " adb -s "+deviceID+" exec-out stat -c '%y'"+" "+path+"/"+fileName);
            // 启动CMD命令
            Process process = processBuilder.start();
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));) {
                String line;
                while ((line = reader.readLine()) != null) {
                    output +=line; // 输出每一行结果
                }
            }
            process.waitFor();
//            System.out.println("CMD命令执行完毕，退出码：" + exitCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return output.replace("'", "");
    }
}
